﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Peradigm.Framework.UI.Web;
using Peradigm.Framework.Domain.Security;

using Liberty.Application.ServiceMemberManagement;
using Liberty.Domain.ServiceMemberManagement;
using Liberty.Domain.CaseManagement;
using Liberty.Domain.GrantManagement;
using Liberty.Application.GrantManagement;
using Liberty.Application.CaseManagement;

public partial class AddGrantWizardView : AbstractLibertyPage
{
    private enum WizardModes
    {
        NewServiceMember,
        NewCase,
        NewGrant
    }

    #region Constants
    private const int
        FIRST_ELEMENT = 0;
    private const string
        POSTBACK_CONTROL_REGISTRATION_SCRIPT = "var wizardClientID = '{0}'; window.document.body.onload = SetPostbackControl;",
        GRANTS_PAGE_URL = "~/GrantManagement/GrantListView.aspx",
        SERVICE_MEMBER_VIEW_URL = "~/ServiceMemberManagement/ServiceMemberView.aspx",
        SERVICE_MEMBER_EDIT_STEP_ID = "wsServiceMemberEditView",
        CASE_EDIT_STEP_ID = "wsCaseEditView",
        GRANT_EDIT_STEP_ID = "wsGrantEditView";
    private const string
        SERVICE_MEMBER_EDIT_CONTROL_URL = "~/ServiceMemberManagement/Controls/ServiceMemberEditView.ascx",
        CASE_EDIT_CONTROL_URL = "~/CaseManagement/Controls/CaseEditView.ascx",
        GRANT_EDIT_CONTROL_URL = "~/GrantManagement/Controls/GrantEditView.ascx";
    #endregion

    #region EventHandlers
    protected override void OnInit( EventArgs e )
    {
        base.OnInit( e );
        this.Title = this.wizAddGrant.HeaderText;
        this.wizAddGrant.HeaderText = string.Empty;
        this.SetWizardMode();
        this.SynchonizeWizardForMode();
    }
    protected void Page_Load( object sender, EventArgs e )
    {
        if ( !this.IsPostBack )
        {
            this.CreateModels();
            this.wizAddGrant.CancelDestinationPageUrl = this.Request.UrlReferrer.AbsoluteUri;
        }
        this.SetModelOnControls();
        this.Page.Title = this.ServiceMemberModel.Rank + " " + this.ServiceMemberModel.CanonicalName;
    }
    protected void wizAddGrant_FinishButtonClick( object sender, WizardNavigationEventArgs e )
    {
        this.SaveModelOnCurrentView();
        this.Transfer( this.RedirectUrl );
    }
    protected void cmdNext_Clicked( object sender, WizardNavigationEventArgs e )
    {
        this.SaveModelOnCurrentView();
    }
    protected override void Render( HtmlTextWriter writer )
    {
        this.ClientScript.RegisterStartupScript( this.GetType(), "PostbackRegistration",
            String.Format( POSTBACK_CONTROL_REGISTRATION_SCRIPT, this.wizAddGrant.ClientID ), true );
        base.Render( writer );
    }
    #endregion

    #region Methods
    protected override void UpdateView()
    {
        base.UpdateView();
        ( (Main)this.Master ).HideAllToolbarActions();
    }
    #endregion 
    #region Properties
    #endregion

    #region Private Methods
    private ServiceMember NewServiceMember()
    {
        ServiceMemberController controller = new ServiceMemberController();
        return controller.NewServiceMemberFor( new UserVariable().User );
    }
    private Case NewCase()
    {
        CaseController caseController = new CaseController();
        Case aCase = caseController.NewCaseFor( new UserVariable().User );
        aCase.ServiceMember = this.ServiceMemberModel;
        return aCase;
    }
    private Grant NewGrant()
    {
        GrantController grantController = new GrantController();
        Grant grant = grantController.NewGrant();
        grant.Case = this.CaseModel;
        return grant;
    }
    private void CreateModels()
    {
        switch ( this.wizAddGrant.ActiveStep.ID )
        {
            case SERVICE_MEMBER_EDIT_STEP_ID :
                this.ServiceMemberModel = this.NewServiceMember();
                this.CaseModel = this.NewCase();
                this.GrantModel = this.NewGrant();
                break;
            case CASE_EDIT_STEP_ID :
                this.CaseModel = this.NewCase();
                this.GrantModel = this.NewGrant();
                break;
            case GRANT_EDIT_STEP_ID :
                this.GrantModel = this.NewGrant();
                break;
        }
    }

    private void SetModelOnControls()
    {
        switch ( this.WizardMode )
        {
            case WizardModes.NewServiceMember:
                this.ucServiceMemberEditView.Model = this.ServiceMemberModel;
                this.ucCaseEditView.Model = this.CaseModel;
                this.ucGrantEditView.Model = this.GrantModel;
                break;
            case WizardModes.NewCase:
                this.ucCaseEditView.Model = this.CaseModel;
                this.ucGrantEditView.Model = this.GrantModel;
                break;
            case WizardModes.NewGrant:
                this.ucGrantEditView.Model = this.GrantModel;
                break;
            default:
                this.ucGrantEditView.Model = this.GrantModel;
                break;
        }
    }
    private void SynchonizeWizardForMode()
    {
        switch ( this.WizardMode )
        {
            case WizardModes.NewCase:
                this.wizAddGrant.WizardSteps.RemoveAt( 0 );
                this.RedirectUrl = SERVICE_MEMBER_VIEW_URL;
                break;
            case WizardModes.NewGrant:
                this.wizAddGrant.WizardSteps.RemoveAt( 0 );
                this.wizAddGrant.WizardSteps.RemoveAt( 0 );
                this.RedirectUrl = SERVICE_MEMBER_VIEW_URL;
                break;
            default:
                this.RedirectUrl = GRANTS_PAGE_URL;
                break;
        }
    }
    private void SetWizardMode()
    {
        if ( this.Request.QueryString["case"] != null )
        {
            this.WizardMode = WizardModes.NewCase;
        }
        else if ( this.Request.QueryString["grant"] != null )
        {
            this.WizardMode = WizardModes.NewGrant;
        }
        else
        {
            this.WizardMode = WizardModes.NewServiceMember;
        }
    }
    private void SaveModelOnCurrentView()
    {
        IView currentView = this.wizAddGrant.ActiveStep.Controls.OfType<IView>().First( control => true );
        if( currentView != null )
        {
            this.AddModelOfCurrentViewToParent();
            currentView.SaveModel();
        }
    }
    private void AddModelOfCurrentViewToParent()
    {
        switch ( this.wizAddGrant.ActiveStep.ID )
        {
            case SERVICE_MEMBER_EDIT_STEP_ID:
                break;
            case CASE_EDIT_STEP_ID:
                this.ServiceMemberModel.AddCase( this.CaseModel );
                break;
            case GRANT_EDIT_STEP_ID:
                this.CaseModel.AddGrant( this.GrantModel );
                break;
        }
    }
    #endregion

    #region Private Properties
    private ServiceMember ServiceMemberModel
    {
        get
        {
            ServiceMemberVariable variable = new ServiceMemberVariable();
            return variable.ServiceMember;
        }
        set
        {
            ServiceMemberVariable variable = new ServiceMemberVariable();
            variable.ServiceMember = value;
        }
    }
    private Case CaseModel
    {
        get
        {
            CaseVariable variable = new CaseVariable();
            return variable.Case;
        }
        set
        {
            CaseVariable variable = new CaseVariable();
            variable.Case = value;
        }
    }
    private Grant GrantModel
    {
        get
        {
            GrantVariable variable = new GrantVariable();
            return variable.Grant;
        }
        set
        {
            GrantVariable variable = new GrantVariable();
            variable.Grant = value;
        }
    }
    private WizardModes WizardMode
    {
        get
        {
            return i_WizardMode;
        }
        set
        {
            i_WizardMode = value;
        }
    }
    private string RedirectUrl
    {
        get
        {
            return i_RedirectUrl;
        }
        set
        {
            i_RedirectUrl = value;
        }
    }
    #endregion

    #region Data Elements
    private WizardModes i_WizardMode;
    private string i_RedirectUrl;
    #endregion
}
